package com.xinlang.excel;


import java.util.Date;

import com.xinlang.util.DateUtil;


/**
 * ���е�Key����String��RawTable
 * 
 */
public class Table extends RawTable<String, String, Object> {
	public Table() {

	}

	public Table(String title) {
		this.title = title;
	}

	// ָ��rowName
	public Table(String title, String rowName) {
		this.title = title;
		this.rowName = rowName;
	}

	public Object increase(String row, String column, String delta) {
		Object object = get(row, column);
		if (object == null) {
			object = "";
		}
		object = object + "" + delta;
		put(row, column, object);
		return object;
	}

	public int getCount(String column, String value, Boolean flag) {
		// flagΪtrueʱ��������ֵΪvalue������
		int count = 0;
		for (String rowName : this.getRowKeys()) {
			if (value.equals(String.valueOf(get(rowName, column)))) {
				count++;
			}
		}
		return count;
	}

	public int getSum(String column) {
		int count = 0;
		for (String rowName : this.getRowKeys()) {
			try {
				count += (Integer) get(rowName, column);
			} catch (Exception e) {
				continue;
			}
		}
		return count;
	}

	public int getSize() {
		return this.getRowKeys().size();
	}

	public Object increase(String row, String column, Number delta) {
		Object object = get(row, column);
		if (object == null) {
			if (delta instanceof Double) {
				object = Double.valueOf(0.0);
			} else if (delta instanceof Integer) {
				object = Integer.valueOf(0);
			} else if (delta instanceof Long) {
				object = Long.valueOf(0L);
			}
		}
		if ((object instanceof Double) && (delta instanceof Double)) {
			object = ((Double) object).doubleValue() + ((Double) delta).doubleValue();
			put(row, column, object);
		} else if ((object instanceof Integer) && (delta instanceof Integer)) {
			object = ((Integer) object).intValue() + ((Integer) delta).intValue();
			put(row, column, object);
		} else if ((object instanceof Long) && (delta instanceof Integer)) {
			object = ((Long) object).longValue() + ((Integer) delta).intValue();
			put(row, column, object);
		} else if ((object instanceof Long) && (delta instanceof Long)) {
			object = ((Long) object).longValue() + ((Long) delta).longValue();
			put(row, column, object);
		} else {
			try {
				object = ((Double) object).doubleValue() + ((Double) delta).doubleValue();
				put(row, column, object);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return object;
	}

	public String getString(String row, String column) {
		Object value = get(row, column);
		return value != null ? value.toString() : null;
	}

	public int getInt(String row, String column) {
		Object value = get(row, column);
		if (value == null) {
			return 0;
		}
		if (value instanceof Long) {
			return ((Long) value).intValue();
		}
		return ((Integer) value).intValue();
	}

	public long getLong(String row, String column) {
		Object value = get(row, column);
		if (value == null) {
			return 0L;
		}
		if (value instanceof Integer) {
			return ((Integer) value).longValue();
		}
		return ((Long) value).longValue();
	}

	public double getDouble(String row, String column) {
		Object value = get(row, column);
		if (value == null) {
			return 0L;
		}
		if (value instanceof Double) {
			return ((Double) value).doubleValue();
		}
		if (value instanceof Float) {
			return ((Float) value).doubleValue();
		}
		return ((Double) value).doubleValue();
	}

	public boolean getBoolean(String row, String column) {
		Object value = get(row, column);
		if (value == null) {
			return false;
		}
		return ((Boolean) value).booleanValue();
	}

	public Date getDate(String row, String column) {
		Object value = get(row, column);
		if (value == null) {
			return null;
		}
		if (value instanceof Date) {
			return (Date) value;
		}
		if (value instanceof String) {
			String dateStr = (String) value;
			if (dateStr.length() <= 10) {
				return DateUtil.parseDate(dateStr);
			} else {
				return DateUtil.parseDatetime(dateStr);
			}
		}
		return null;
	}

	public void put(String row, String column, Date date) {
		put(row, column, date != null ? DateUtil.datetimeToString(date) : null);
	}

	public static void main(String[] args) {
		Table table = new Table("�����˷���", "ID");
		table.put("1497035431", "1", "1");
		Object object = table.get("1497035431", null);
		System.err.println(object);
	}
}
